<template>
  <div>
    <el-dialog title="分配角色" :visible="isShow" @close="btnCancel">
      <el-checkbox-group v-model="roleListId">
        <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">
          {{ role.name }}
        </el-checkbox>
      </el-checkbox-group>
      <template slot="footer">
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="btnOk">确定</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { updateRole } from '@/api/employees'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [],
      roleListId: [],
      employId: ''
    }
  },
  async created() {
    const { rows } = await getRoleList()
    this.roleList = rows
  },
  // 数据回显
  methods: {
    async getRoleById(id) {
      this.employId = id
      const res = await getUserDetailById(id)
      this.roleListId = res.roleIds
    },
    // 取消弹窗
    btnCancel() {
      // 清空数据
      this.roleListId = []
      this.$emit('update:isShow', false)
    },
    async btnOk() {
      await updateRole({
        id: this.employId,
        roleIds: this.roleListId
      })
      this.$message.success('操作成功')
      this.$emit('update:isShow', false)
    }
  }
}
</script>

<style>

</style>
